<?php

/*

@author Indigo
@version 0.3
@date 5/8/2011
@last_update 5/9/2011
@copyright 2011


Release notes v 0.3

* Deprecate imgDectecter. Not safe to be splitting strings and may not ensure validity is double '.' and included in img name
* Deprecate isImg function
* Added a new isImage function with Regex filtering
* Unset the first element of the array

Release notes v 0.2

* Added image detection filter. Filters image MIME types
* Added Filename spliter to send extension to image type checker

Release notes v 0.1 

* Create method to traverse through directory for files and jsonify filenames


The following file creates a json file of all the images in the image folder

*/

$dir = "images/slideshow"; // image folder
$stack[] = array();
unset($stack[0]); // remove the first element from the array

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
			if(!file_exists($file) && isImage($file))
				array_push($stack,$file); //push files to stack
        }
        closedir($dh);
    }
	
	
	//Backword compatibility support with PHP5 -> PHP4
	if (!function_exists('json_encode')) {
		function json_encode($content) {
			require_once 'ext/JSON.php';
			$json = new Services_JSON;
			return $json->encode($content);
		}
	}
	
	//encode associative array to json
	echo json_encode($stack,JSON_FORCE_OBJECT);
}

/**
* The following function retrieves the retrieve the extension of a file and checks if it's a valid img type
* @deprecated poor image checking technique
**/
function imgDetecter($n) {
	$res = preg_split("/\./", $n);
	if(count($res) == 2) {
		return isImg($res[1]);
	}
}

/**
* The following function checks if a file is an image
* @deprecated use isImage instead
* @see #isImage
* @return boolean	
**/
function isImg($n) {
	$array = array("png","jpg","jpeg","bmp","gif");
	foreach($array as $i => $value) {
		if($array[$i] == $n) {
			return true;
		}
	}
	return false;
}

/**
* The following function checks if a file is a valid image type
*/
function isImage($n) {
	//enhanced version of image validation function
	return preg_match("/([a-zA-Z0-9._-])+(\.png|\.jpg|\.jpeg|\.bmp|\.gif)/i",$n);
}

?>